package com.module.my.model

import androidx.annotation.ColorRes
import cn.leancloud.LCObject
import cn.leancloud.LCQuery
import com.module.app.base.BaseViewModel
import com.module.app.bean.ListViewBean
import com.module.app.ext.modifyUserFlow
import com.module.app.network.MyBaseResponse
import com.module.base.ext.request
import com.module.base.utils.CommonUtils
import com.module.base.utils.ToastUtils
import com.module.frame.ext.postEvent
import com.module.my.R
import com.module.my.bean.VipConfigBean
import com.module.my.bean.VipContentBean
import com.module.my.bean.VipTitleBean
import com.module.my.bus.PayVipBus
import com.module.third.learcloud.lcQuery
import com.module.third.learcloud.lcQuerySingle
import com.module.third.learcloud.lcSave
import com.module.third.learcloud.SUCCESS_CODE
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.withContext
import java.math.BigDecimal

class VipViewModel : BaseViewModel() {

    /**
     * 价格信息
     */
    val priceFlow: MutableSharedFlow<PayVipBus> by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
        MutableSharedFlow()
    }

    /**
     * 获取vip配置成功
     */
    val vipConfigSucFlow: MutableSharedFlow<MutableList<VipConfigBean>> by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
        MutableSharedFlow()
    }

    fun getData() {
        request({
            withContext(Dispatchers.IO) {
                val list: MutableList<Any> = ArrayList<Any>()
                list.add(VipTitleBean("权益对比"))
                list.add(getBean(true, "功能", "免费版", "会员版"))
                list.add(getBean("加密图片", "无限制", "无限制"))
                list.add(getBean("加密视频", "无限制", "无限制"))
                list.add(getBean("云相册", "赠送100张/3月", "具体在顶部查看", 0, 0, R.color.bg_red2))
                list.add(getBean("加密电话", "无限制", "无限制"))
                list.add(getBean("加密网址", "无限制", "无限制"))
                list.add(getBean("加密账号", "无限制", "无限制"))
                list.add(getBean("我的应用", "无限制", "无限制"))
                list.add(getBean("工具·视频提起", "5次/天", "无限制", 0, 0, R.color.bg_red2))
                list.add(getBean("工具·区域裁剪", "5次/天", "无限制", 0, 0, R.color.bg_red2))
                list.add(getBean("工具·视频转GIF", "5次/天", "无限制", 0, 0, R.color.bg_red2))
                list.add(getBean("工具·视频剪切", "5次/天", "无限制", 0, 0, R.color.bg_red2))
                list.add(getBean("工具·视频倒放", "5次/天", "无限制", 0, 0, R.color.bg_red2))
                list.add(getBean("指纹解锁", "无限制", "无限制"))
                list.add(getBean("熄屏切换", "无限制", "无限制"))
                list.add(getBean("伪装应用", "无限制", "无限制"))
                list.add(getBean("广告", "概率显示", "无", 0, 0, R.color.bg_red2))
                list.add(getBean("伪密码", "部分限制", "无限制", 0, 0, R.color.bg_red2))
                list.add(getBean("记录点滴", "无限制", "支持视频图片混合", 0, 0, R.color.bg_red2))
                list.add(getBean("加密电话·云端备份", "无限制", "无限制"))
                list.add(getBean("加密网址·云端备份", "无限制", "无限制"))
                list.add(getBean("加密账号·云端备份", "无限制", "无限制"))
                list.add(getBean("独立密码", "限制20张", "无限制", 0, 0, R.color.bg_red2))
                list.add(getBean("主题", "部分限制", "无限制", 0, 0, R.color.bg_red2))
                list.add(getBean("背景图", "不支持", "无限制", 0, 0, R.color.bg_red2))
                list.add(getBean("自动夜间模式", "部分限制", "无限制", 0, 0, R.color.bg_red2))
                list.add(getBean("自定义排序", "不支持", "支持", 0, 0, R.color.bg_red2))

                list.add(VipTitleBean("后续开发"))

                list.add(getBean("图片搜索", "待开发", "待开发"))
                return@withContext MyBaseResponse(SUCCESS_CODE, "", list)
            }
        }, {
            postEvent(listFlow, ListViewBean(true, it, true))
        }, {
            postEvent(errorFlow, it)
        }, false)
    }

    fun getVipConfig() {
        request({
            val tClass = VipConfigBean::class.java
            val query = LCQuery<LCObject>(tClass.simpleName)
            lcQuery(query, tClass)
        }, {
            check(it != null) {
                CommonUtils.getString(R.string.error_unknow)
            }
            if (!it.isNullOrEmpty()) {
                //默认第一个选中
                it[0].isSelect = true
            }
            postEvent(vipConfigSucFlow, it)
        }, {}, false)
    }

    fun getPay(price: BigDecimal, vipConfigBean: VipConfigBean) {
        request({
            val tClass: Class<com.module.app.bean.PayBean> = com.module.app.bean.PayBean::class.java
            val query = LCQuery<LCObject>(tClass.simpleName)
            lcQuerySingle(query, tClass)
        }, {
            check(it != null) {
                "获取支付信息失败"
            }
            postEvent(priceFlow, PayVipBus(price, it, vipConfigBean))
        }, {
            ToastUtils.showShort("获取支付信息失败")
        }, true)
    }

    fun buyVip(bean: VipConfigBean) {
        request({
            val userBean = com.module.app.utils.UserUtils.getUserBean()
            userBean.vipSource = com.module.app.bean.IType.IVipSource.buy
            if (bean.day > 0) {
                userBean.vipExpiration =
                    System.currentTimeMillis() + 1000L * 60 * 60 * 24 * bean.day
            } else {
                userBean.vipExpiration = -1
            }
            lcSave(userBean, userBean.objectId, com.module.app.bean.UserBean::class.java)
        }, {
            check(it != null) {
                CommonUtils.getString(R.string.error_unknow)
            }
            ToastUtils.showShort("购买成功")
            postEvent(modifyUserFlow, true)
        }, {
            ToastUtils.showShort(it.msg)
        }, true)
    }


    private fun getBean(
        isTitle: Boolean,
        name1: String,
        name2: String,
        name3: String
    ): VipContentBean {
        val bean = VipContentBean()
        bean.isTitle = isTitle
        bean.name1 = name1
        bean.name2 = name2
        bean.name3 = name3
        return bean
    }

    private fun getBean(
        name1: String,
        name2: String,
        name3: String
    ): VipContentBean {
        return getBean(false, name1, name2, name3)
    }

    private fun getBean(
        isBold: Boolean,
        name1: String,
        name2: String,
        name3: String,
        @ColorRes colorRes1: Int,
        @ColorRes colorRes2: Int,
        @ColorRes colorRes3: Int
    ): VipContentBean {
        val bean = getBean(isBold, name1, name2, name3)
        bean.colorRes1 = colorRes1
        bean.colorRes2 = colorRes2
        bean.colorRes3 = colorRes3
        return bean
    }

    private fun getBean(
        name1: String,
        name2: String,
        name3: String,
        @ColorRes colorRes1: Int,
        @ColorRes colorRes2: Int,
        @ColorRes colorRes3: Int
    ): VipContentBean {
        return getBean(false, name1, name2, name3, colorRes1, colorRes2, colorRes3)
    }
}